package com.openbravo.data.loader.dialect.PostgreSQL;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DBSession;
import com.openbravo.data.loader.dialect.AbstractDataBase;
import e.odbo.DB;

public class PostgreSQL extends AbstractDataBase<PostgreSQL> {
    private static String JDBCDriver="org.postgresql.Driver";

    public static String NAME="postgresql";

    public static class TYPE{

    }


    public PostgreSQL() {
        super(NAME);
        this.port=5432;
    }


    public static DB connectLocal(String user,String password,String database)throws BasicException{
        return connect("localhost",5432,database,user,password);
    }

    public static DB connect(String host,int port,String database,String user,String password)throws BasicException {
        try {
            Class.forName(JDBCDriver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new BasicException(e.getMessage());
        }
        String url = String.format("jdbc:postgresql://%s:%d/%s", host, port, database);
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    public static PostgreSQL C() throws BasicException {
        try {
            Class.forName(JDBCDriver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new BasicException(e.getMessage());
        }
        return new PostgreSQL();
    }


    public  PostgreSQL ssl(boolean value){
        this.connectProp.put("ssl",String.valueOf(value));
        return this;
    }
}
